package org.lord.slark.numbers.activity;

import android.app.DatePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import org.lord.slark.numbers.R;
import org.lord.slark.numbers.db.LocalDB;
import org.lord.slark.numbers.entity.ConferenceNumber;

import java.util.Calendar;

public class AddConferenceData extends AppCompatActivity implements View.OnClickListener {
    private TextView tvtotal, dptime;
    private EditText etbrother, etsister;
    private Button btncommit;
    private Spinner sptype;
    private ImageView btnback;
    private Calendar mCalendar;
    private int mDefaultYear, mDefaultMonth, mDefaultDay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_add_conference_data);
        mCalendar = Calendar.getInstance();
        initView();
    }

    private void initView() {
        tvtotal = (TextView) findViewById(R.id.tvtotal);
        dptime = (TextView) findViewById(R.id.dptime);
        etbrother = (EditText) findViewById(R.id.etbrother);
        etsister = (EditText) findViewById(R.id.etsister);
        btncommit = (Button) findViewById(R.id.btncommit);
        sptype = (Spinner) findViewById(R.id.sptype);
        btnback = (ImageView) findViewById(R.id.btnback);
        btncommit.setOnClickListener(this);
        dptime.setOnClickListener(this);
        btnback.setOnClickListener(this);
        etbrother.addTextChangedListener(twatcher);
        etsister.addTextChangedListener(twatcher);
        mDefaultYear = mCalendar.get(Calendar.YEAR);
        mDefaultMonth = mCalendar.get(Calendar.MONTH);
        mDefaultDay = mCalendar.get(Calendar.DAY_OF_MONTH);
        dptime.setText(mDefaultYear + "/" + (mDefaultMonth + 1) + "/" + mDefaultDay);
    }

    private TextWatcher twatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String sbrother = etbrother.getText().toString().trim();
            String ssister = etsister.getText().toString().trim();
            int bnumber = 0;
            int snumber = 0;
            if (sbrother != null && sbrother.length() != 0) {
                bnumber = Integer.valueOf(sbrother);
            }
            if (ssister != null && ssister.length() != 0) {
                snumber = Integer.valueOf(ssister);
            }
            if (bnumber+snumber != 0) {
                tvtotal.setVisibility(View.VISIBLE);
                tvtotal.setText(String.format(getString(R.string.stotal), bnumber+snumber));
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };
    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btncommit:
                String sbrother = etbrother.getText().toString().trim();
                String ssister = etsister.getText().toString().trim();
                int bnumber = 0;
                int snumber = 0;
                if (sbrother != null && sbrother.length() != 0) {
                    bnumber = Integer.valueOf(sbrother);
                }
                if (ssister != null && ssister.length() != 0) {
                    snumber = Integer.valueOf(ssister);
                }
                if (bnumber == 0 && snumber == 0) {
                    Toast.makeText(this, getString(R.string.toastnumber), Toast.LENGTH_SHORT).show();
                    return;
                }
                int total = bnumber + snumber;
                String type = sptype.getSelectedItem().toString();
                String time = dptime.getText().toString().trim();

                String insertSql = "insert into " + ConferenceNumber.TABLE_NAME + " values(NULL, ?, ?, ?, ?, ?);";
                try {
                    LocalDB.getInstance(this).getWritableDatabase().execSQL(insertSql, new Object[]{
                            type, time, total, bnumber, snumber
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
                finish();
                break;
            case R.id.dptime:
                DatePickerDialog dpd = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        mDefaultYear = year;
                        mDefaultMonth = monthOfYear;
                        mDefaultDay = dayOfMonth;
                        dptime.setText(year + "/" + (monthOfYear + 1) + "/" + dayOfMonth);
                    }
                }, mDefaultYear, mDefaultMonth, mDefaultDay);
                dpd.show();
                break;
            case R.id.btnback:
                onBackPressed();
                break;
            default:
                break;
        }
    }

}
